<?php

namespace App\Remote\MeiTuan;

use App\Exceptions\BaseException;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class MeiTuanTakeAwayOrderRemote extends Remote
{
    /**
     * @inheritDoc
     */
    static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "https://waimaieapp.meituan.com";
    }


    /**
     * @param string $wmPoiId
     * @param string $cookie
     * @return array
     */
    public static function getShopQueryCookie(string $wmPoiId, string $cookie): array
    {
        parse_str(str_replace(["; ", ";"], "&", $cookie), $params);
        return [
            "Cookie" => vsprintf("token=%s; acctId=%s; wmPoiId=%s", [
                $params["token"],
                $params["acctId"],
                $wmPoiId
            ])
        ];
    }

    /**
     * @param string $cookie
     * @param string $wmPoiId
     * @param string $settleBillStartDate
     * @param string $settleBillEndDate
     * @param int $pageNum
     * @param int $pageSize
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function historySettleBillList(
        string $cookie,
        string $wmPoiId,
        string $settleBillStartDate,
        string $settleBillEndDate,
        int $pageNum = 1,
        int $pageSize = 10
    ) {
        return self::customRequestV3(
            vsprintf(
                "/finance/pc/api/poiSettleBill/historySettleBillList?settleBillStartDate=%s&settleBillEndDate=%s&pageSize=%d&pageNo=%d",
                [
                    $settleBillStartDate,
                    $settleBillEndDate,
                    $pageSize,
                    $pageNum
                ]
            ),
            RequestAlias::METHOD_GET,
            null,
            self::getShopQueryCookie($wmPoiId, $cookie)
        );
    }

    public static function poiAccountFlows(
        string $cookie,
        string $wmPoiId,
        int $pageNum = 1,
        int $pageSize = 10
    ) {
        return self::customRequestV3(
            vsprintf(
                "/finance/waimai/accountFlow/api/poiAccountFlows?wmPoiId=%s&appType=3&beginDate&endDate&direction&pageNo=%d&pageSize=%d&acctType=1008&financeAccountId=-1",
                [
                    $wmPoiId,
                    $pageNum,
                    $pageSize
                ]
            ),
            RequestAlias::METHOD_GET,
            null,
            self::getShopQueryCookie($wmPoiId, $cookie)
        );
    }

    /**
     * 结算账单详情
     *
     * @param string $cookie
     * @param string $wmPoiId
     * @param string $settleBillId
     * @return mixed
     * @throws BaseException
     * @throws GuzzleException
     */
    public static function settleBillDetail(string $cookie, string $wmPoiId, string $settleBillId)
    {
        return self::customRequestV3(
            vsprintf(
                "/finance/v4/h5/api/settleBill/detail?wmPoiId=%s&settleBillId=%s",
                [
                    $wmPoiId,
                    $settleBillId
                ]
            ),
            RequestAlias::METHOD_GET,
            null,
            self::getShopQueryCookie($wmPoiId, $cookie)
        );
    }
}
